var toastR = {
	//订单相关 0000~0999
	//手机充值
	m0001: '手机号码格式错误',
	m0002: '请选择充值金额',
	m0003: '订单提交中',
	m0004: '订单提交成功',
	//购物车
	m0005: '只能选择同一个商家的商品进行结算',
	m0006: '还没有选择商品',
	m0007: '确定要从购物车删除该商品么？',
	m0008: '确定要清除失效商品么？',
	//订单评论
	m0009: '只能上传4张照片',
	m0010: '上传图片不能超过4张',
	m0011: '快给店铺服务态度打个分吧',
	m0012: '快给物流服务打个分吧',
	m0013: '快给商品{0}打个分吧',
	m0014: '评论成功',
	m0015: '提交评论失败',
	//订单操作相关
	m0016: '确定要取消订单么？',
	m0017: '取消订单成功',
	m0018: '确定要删除订单么？',
	m0019: '删除订单成功',
	m0020: '确定要提交站长代付申请吗？',
	m0021: '站长代付申请已提交',
	m0022: '是否确认收货？请确定已收到货物后再确认。',
	m0023: '已确认收货',
	m0024: '确定要延长收货时间吗？订单只能延长一次收货时间。',
	m0025: '延长收货成功',
	m0026: '订单号复制成功',
	//订单支付
	m0027: '请先选择一个支付方式',
	m0028: '确定要提交站长代付申请吗？申请后由收货地址绑定的站长帮您付款。',
	m0029: '未安装选择支付App',
	m0030: '支付失败',
	m0031: '支付成功',
	m0032: '获取支付环境失败，请重试',
	//下单
	m0033: '获取运费错误',
	m0034: '请选择站点客户', //站长情况
	m0035: '请选择收货地址', //普通用户
	m0036: '订单提交成功',
	//快捷支付
	m0037: '绑定信息验证成功，短信以下发',
	m0038: '银行卡绑定成功',
	m0039: '支付参数错误',
	//售后
	m0040: '只能上传3张照片',
	m0041: '上传图片不能超过3张',
	m0042: '请选择售后类型',
	m0043: '请选择是否收到货物',
	m0044: '请选择退款原因',
	m0045: '退款金额不能大于订单金额',
	m0046: '提交售后服务申请成功',
	m0047: '提交售后服务失败',
	m0048: '请填写退货物流公司名称',
	m0049: '请填写退货物流单号',
	m0050: '退货物流信息提交成功，请等待商家确认',
	//支付提示
	m0051: '订单需在1天内完成支付，超时会自动取消',

	//个人中心 1001~1999
	//收货地址
	m1001: '联系人不能为空',
	m1002: '手机号码不能为空',
	m1003: '手机号格式不正确',
	m1004: '请选择省、市、区（县）',
	m1005: '请选择镇',
	m1006: '请选择村',
	m1007: '没有该区域对应的服务站！无法绑定地址',
	m1008: '请选择站点',
	m1009: '收货地址添加成功',
	m1010: '收货地址保存成功',
	m1011: '站点数据错误', //二维码数据错误
	m1012: '确定删除改地址吗？',
	m1013: '删除成功',
	m1014: '删除失败',
	m1015: '默认地址设置成功',
	m1016: '默认地址设置失败',
	m1044: '确定放弃填写收货地址？',
	//个人信息
	m1017: '请输入修改昵称',
	m1018: '昵称修改成功',
	m1019: '性别修改成功',
	//登录注册重置密码
	m1020: '请输入手机号',
	m1021: '请输入密码',
	m1022: '手机号格式不正确',
	m1023: '登录成功',
	m1024: '用户名或密码错误',
	m1025: '请先阅读贵农网注册协议，同意并进行下一步',
	m1026: '短信已下发，请注意查收',
	m1027: '距离下一次发短信还需等待{0}秒',
	m1028: '注册成功',
	m1029: '密码重置成功，请重新登录',
	//意见建议
	m1030: '别忘了填写意见建议内容哦',
	m1031: '意见建议提交成功，感谢您的支持',
	//站长
	m1032: '请填写客户姓名',
	m1033: '请填写正确的手机号码',
	m1034: '保存客户成功',
	m1035: '添加客户成功',
	m1036: '确定要拒绝订单支付申请么？请先与用户协商一致后再决定哦。',
	m1037: '已拒绝代付申请',
	m1038: '确定要删除代付申请记录么？',
	m1039: '代付申请记录删除成功',
	m1040: '站点人员无法绑定其他站点地址哦',
	//密码验证
	m1041: '密码长度不能少于6位',
	m1042: '密码不能全是数字',
	//二维码
	m1043: '未识别到二维码',

	//商品相关 2001~2999
	m2001: '请输入搜索关键词',
	m2002: '获取商品数据失败',
	m2003: '站长推荐添加成功',
	m2004: '请选择商品规格',
	m2005: '已添加至购物车',

	//公用相关 9001~9999
	m9001: '用户未登录',
	m9002: '已进入没有网络的异次元',
	m9003: '网络发生异常',

};

document.addEventListener('plusready', function() {
	plus.navigator.setStatusBarBackground('#6FCB15');
}, false);

if(!NodeList.prototype.forEach) {
	NodeList.prototype.forEach = Array.prototype.forEach;
}
var gnApp = {
	showWaiting: function(title) {
		title = title || '数据加载中';
		mui.os.plus && plus.nativeUI.showWaiting(title);
	},
	closeWaiting: function() {
		mui.os.plus && plus.nativeUI.closeWaiting();
	},
	alert: function(title, content, btn, callback) {
		mui.os.plus && plus.nativeUI.alert(content, function(e) {
			callback && callback();
		}, title, btn);
	},
	toast: function(msg, options) {
		mui.os.plus && plus.nativeUI.toast(msg, options);
	},
	confirm: function(message, confirmCB, title, buttons) {
		mui.os.plus && plus.nativeUI.confirm(message, confirmCB, title, buttons);
	},
	hasClass: function(obj, cls) {
		return obj.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
	},
	addClass: function(obj, cls) {
		if(!gnApp.hasClass(obj, cls)) {
			obj.className += " " + cls;
		}
	},
	removeClass: function(obj, cls) {
		if(gnApp.hasClass(obj, cls)) {
			var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
			obj.className = obj.className.replace(reg, ' ');
		}
	},
	toggleClass: function(obj, cls) {
		if(gnApp.hasClass(obj, cls)) {
			removeClass(obj, cls);
		} else {
			addClass(obj, cls);
		}
	}
};

gnApp.openMask = function(maskView, popView, clickAction) {
	if(clickAction != 'hide' && clickAction != 'close') {
		throw 'mask点击方法只能是hide或close';
	}
	maskView.setStyle({
		mask: "rgba(0,0,0,0.5)"
	});
	// 用户点击后
	var onMaskClick = function() {

		mui.fire(popView, 'close');
	};
	var onPopClose = function() {
		maskView.setStyle({
			mask: "none"
		});
	};
	maskView.removeEventListener("maskClick", onMaskClick);
	maskView.addEventListener("maskClick", onMaskClick, false);
	popView.removeEventListener(clickAction, onPopClose);
	popView.addEventListener(clickAction, onPopClose, false);
};

gnApp.openProDetail = function(proId, second) {
	//	var detailView1 = plus.webview.getWebviewById('goods_detail_1');
	//	mui.fire(detailView1, 'openProDetail', {
	//		id: proId,
	//		second: second
	//	});
	gnApp.TrigEvent('OpenGoods', {
		'goodsId': proId
	});
	var detailView1 = plus.webview.getWebviewById('goods_detail');
	if(detailView1) {
		//		detailView1.close('none');
		gnApp.showWaiting();
		mui.fire(detailView1, 'reload', {
			proId: proId
		});
	} else {
		gnApp.showWaiting();

		var styles = {};
		// 在Android5以上设备，如果默认没有开启硬件加速，则强制设置开启
		if(!plus.webview.defaultHardwareAccelerated() && parseInt(plus.os.version) >= 5) {
			styles.hardwareAccelerated = true;
		}
		var detailView = plus.webview.create('/pages/goods/detail.html', 'goods_detail', styles, {
			proId: proId
		});
	}
};

gnApp.openProDetail2 = function(proId, second) {
	var detailView2 = plus.webview.getWebviewById('goods_detail_2');
	mui.fire(detailView2, 'openProDetail', {
		id: proId,
		second: second
	});
};

gnApp.openSearchList = function(searchParam) {
	var styles = {};
	gnApp.TrigEvent('OpenSearchList', {
		'searchParam': searchParam
	});
	// 在Android5以上设备，如果默认没有开启硬件加速，则强制设置开启
	if(!plus.webview.defaultHardwareAccelerated() && parseInt(plus.os.version) >= 5) {
		styles.hardwareAccelerated = true;
	}
	gnApp.showWaiting();
	var searchView1 = plus.webview.create('/pages/goods/search.html', 'search', styles, {
		searchParam: searchParam
	});

};

gnApp.openStationBind = function(stationParam) {
	if(!gnAuth.isLogin()) {
		gnAuth.openLogin();
		return;
	}
	if(gnAuth.isStationUser()) {
		gnApp.toast(toastR.m1040);
		return;
	}
	gnApp.TrigEvent('OpenStationBind', {
		'stationParam': stationParam
	});
	var styles = {};
	// 在Android5以上设备，如果默认没有开启硬件加速，则强制设置开启
	if(!plus.webview.defaultHardwareAccelerated() && parseInt(plus.os.version) >= 5) {
		styles.hardwareAccelerated = true;
	}
	gnApp.showWaiting();
	var searchView1 = plus.webview.create('/pages/user/address_code.html', 'address_code', styles, {
		stationParam: stationParam
	});

};

gnApp.openShop = function(shopId) {
	gnApp.TrigEvent('OpenShop', {
		'shopId': shopId
	});
	var hasView = plus.webview.getWebviewById('shop_' + shopId);
	if(hasView) {
		hasView.show();
	} else {
		gnApp.openPage('shop.html', 'shop_' + shopId, {
			shopId: shopId
		});
	}
};

gnApp.openActPage = function(pageId) {
	gnApp.TrigEvent('OpenActPage', {
		'pageId': pageId
	});
	var hasView = plus.webview.getWebviewById('actPage_' + pageId);
	if(hasView) {
		hasView.show('slide-in-right', 200);
	} else {
		gnApp.openPage('/act_page.html', 'actPage_' + pageId, {
			pageId: pageId
		});
	}
};

gnApp.openCart = function(callback) {
	if(!gnAuth.isLogin()) {
		gnAuth.openLogin();
		return;
	}
	gnApp.openPage('/pages/order/cart.html', 'cart');
	//	var cart = plus.webview.getWebviewById('cart');
	//	mui.fire(cart, 'openCartList', {});
	callback && callback();
};

gnApp.openOrderSubmit = function(orderParam) {
	gnApp.TrigEvent('OpenOrderSubmit', {
		'orderParam': orderParam
	});
	var orderTitleView = plus.webview.getWebviewById('order_title');
	mui.fire(orderTitleView, 'openSubView', {
		page: 'order_submit.html',
		pageId: 'order_submit',
		title: '确认订单',
		subViewParam: orderParam
	});
};

gnApp.openOrderList = function(mode) {
	var orderTitleView = plus.webview.getWebviewById('order_title');
	mui.fire(orderTitleView, 'openSubView', {
		page: 'order_list.html',
		pageId: 'order_list',
		title: '订单列表',
		subViewParam: {
			mode: mode
		}
	});
};

gnApp.closeOrderSubmit = function() {
	var orderTitleView = plus.webview.getWebviewById('order_title');
	mui.fire(orderTitleView, 'closePage');
};

gnApp.toHome = function() {
	var pageIds = [
		'goods_detail_1',
		'goods_detail_2',
		'goods_search_1',
		'goods_search_2',
		'cart',
		'order_title'
	]
	for(var i = 0; i < pageIds.length; i++) {
		var page = plus.webview.getWebviewById(pageIds[i]);
		mui.fire(page, 'closePage');
	}

	var homePage = plus.webview.getLaunchWebview();
	mui.fire(homePage, 'tohome');
};

// 打开页面，显示系统原生loading
gnApp.openPage = function(pageUrl, pageId, extras, autoShow, showAniType, showDuration) {
	gnApp.TrigEvent('OpenPage', {
		'PageUrl': pageUrl,
		extras: extras
	});
	gnApp.showWaiting('拼命加载中');
	var styles = {};
	// 在Android5以上设备，如果默认没有开启硬件加速，则强制设置开启
	if(!plus.webview.defaultHardwareAccelerated() && parseInt(plus.os.version) >= 5) {
		styles.hardwareAccelerated = true;
	}
	var newView = plus.webview.create(pageUrl, pageId, styles, extras);
	if(autoShow) {
		newView.addEventListener("loaded", function() {
			gnApp.closeWaiting();
			showAniType = showAniType || 'slide-in-right';
			showDuration = showDuration || 300;
			newView.show(showAniType, showDuration);
		}, false);
	}
};

gnApp.addToClipboard = function(content) {
	if(mui.os.android) {
		var Context = plus.android.importClass("android.content.Context");
		var main = plus.android.runtimeMainActivity();
		var clip = main.getSystemService(Context.CLIPBOARD_SERVICE);
		plus.android.invoke(clip, "setText", content);
	} else {
		var UIPasteboard = plus.ios.importClass("UIPasteboard");
		var generalPasteboard = UIPasteboard.generalPasteboard();
		generalPasteboard.setValueforPasteboardType(content, "public.utf8-plain-text");
	}
};

gnApp.openAddressSelect = function(callbackPageId) {
	gnApp.showWaiting();
	var styles = {};
	// 在Android5以上设备，如果默认没有开启硬件加速，则强制设置开启
	if(!plus.webview.defaultHardwareAccelerated() && parseInt(plus.os.version) >= 5) {
		styles.hardwareAccelerated = true;
	}
	var addressList = plus.webview.create('../user/address_list.html', 'address_list', styles, {
		isSelectAddr: true,
		callbackPageId: callbackPageId
	});
	addressList.addEventListener("loaded", function() {
		gnApp.closeWaiting();
		addressList.show('slide-in-right', 200);
	}, false);
};

gnApp.openCustomerSelect = function(callbackPageId) {
	gnApp.showWaiting();
	var styles = {};
	// 在Android5以上设备，如果默认没有开启硬件加速，则强制设置开启
	if(!plus.webview.defaultHardwareAccelerated() && parseInt(plus.os.version) >= 5) {
		styles.hardwareAccelerated = true;
	}
	var addressList = plus.webview.create('../master/customer_list.html', 'customer_list', styles, {
		isSelectCust: true,
		callbackPageId: callbackPageId
	});
	addressList.addEventListener("loaded", function() {
		gnApp.closeWaiting();
		addressList.show('slide-in-right', 200);
	}, false);
};

gnApp.rem2Px = function(rem) {
	var px = window.lib.flexible.rem2px(rem);
};

gnApp.orderStatusDic = {
	'1': 'pay',
	'2': 'delivery',
	'3': 'dispatch',
	'4': 'finish',
	'5': 'close'
};
gnApp.orderStatusDesc = {
	'1': 'pay',
	'2': 'delivery',
	'3': 'dispatch',
	'4': 'finish',
	'5': 'close'
};

//验证手机号
gnApp.validatePhone = function(phone) {
	var myreg = /^1[34578]\d{9}$/;
	if(!myreg.test(phone)) {
		return false;
	}
	return true;

};

//验证密码
gnApp.validatePassWord = function(passWords) {
	if(passWords.length < 6) {
		gnApp.toast(toastR.m1041);
		return false;
	}
	if(/^[0-9]*[1-9][0-9]*$/.test(passWords)) {
		gnApp.toast(toastR.m1042);
		return false;
	}
	return true;
};

gnApp.getHourSpan = function(startDate, endDate) {
	return Math.round((startDate - endDate) / (1000 * 60 * 60));
};

gnApp.getDayAndHourSpan = function(startDate, endDate) {
	var totalHour = gnApp.getHourSpan(startDate, endDate);
	return {
		day: Math.floor(totalHour / 24),
		hour: totalHour % 24
	};
};

gnApp.callPhone = function(phoneNumber, title) {
	title = title || '拨打电话';
	gnApp.confirm(phoneNumber, function(e) {
		if(e.index == 0) {
			plus.device.dial(phoneNumber, false);
		}
	}, title, ["确定", "取消"]);
};

gnApp.callService = function() {
	gnApp.callPhone('4001156060', '联系客服');
};

gnApp.qrCbFunc = null;
gnApp.qrScan = function(callback) {
	var currentView = plus.webview.currentWebview();
	if(gnApp.qrCbFunc) {
		window.removeEventListener('onQrCode', gnApp.qrCbFunc);
	}
	gnApp.qrCbFunc = function(event) {
		callback && callback(event.detail.result);
	};
	window.addEventListener('onQrCode', gnApp.qrCbFunc);
	gnApp.openPage('/qr_code.html', 'qr_code', {
		callbackId: currentView.id
	}, false);
};

gnApp.refreshUser = function() {
	var userView = plus.webview.getWebviewById('user.html');
	if(userView) {
		mui.fire(userView, 'usercenter');
	}
};

var gnUrl = {};
gnUrl.allowUrl = [
	/http:\/\/.*\.coopbuy\.com.*/,
	/http:\/\/.*\.guinong\.com.*/,
	/http:\/\/.*\.guinong360\.com.*/
];
gnUrl.exec = function(rawUrl, title) {
	var orgUrl = rawUrl;
	rawUrl = rawUrl.toLowerCase();
	if(rawUrl.indexOf('http') > -1) {
		var match = false;
		gnUrl.allowUrl.forEach(function(reg) {
			if(reg.test(rawUrl)) {
				match = true;
			}
		});
		if(!match) {
			return;
		}
	}
	if(rawUrl.indexOf('/shop/home/') > -1) {
		var shopId = rawUrl.substr(rawUrl.indexOf('/shop/home/') + 11, rawUrl.length - rawUrl.indexOf('/shop/home/'));
		indexOpenShopById(shopId);
		return true;
	} else if(rawUrl.indexOf('/category') > -1) {
		gnApp.openPage('/pages/goods/category.html', 'category', {}, false);
		return true;
	} else if(rawUrl.indexOf('/goods/detail/') > -1) {
		var pId = rawUrl.substr(rawUrl.indexOf('/goods/detail/') + 14, rawUrl.length - rawUrl.indexOf('/goods/detail/'));
		gnApp.openProDetail(pId);
		return true;
	} else if(rawUrl.indexOf('/topic/detail/') > -1) {
		var tId = rawUrl.substr(rawUrl.indexOf('/topic/detail/') + 14, rawUrl.length - rawUrl.indexOf('/topic/detail/'));
		indexOpenTopic(tId);
		return true;
	} else if(rawUrl.indexOf('/search') > -1) {
		var paramStr = orgUrl.split('?')[1];
		var params = paramStr.split('&');
		var searchParam = {};
		params.forEach(function(v) {
			var splParam = v.split('=');
			if(splParam.length == 2) {
				searchParam[splParam[0]] = decodeURIComponent(splParam[1]);
			}
		});
		gnApp.openSearchList(searchParam);
		return true;
	} else if(rawUrl.indexOf('/station') > -1) {
		var paramStr = orgUrl.split('?')[1];
		var params = paramStr.split('&');
		var stationParam = {};
		params.forEach(function(v) {
			var splParam = v.split('=');
			if(splParam.length == 2) {
				stationParam[splParam[0]] = decodeURIComponent(splParam[1]);
			}
		});
		gnApp.openStationBind(stationParam);
		return true;
	} else if((rawUrl.indexOf('https://') == -1 || rawUrl.indexOf('http://') == -1)) {
		var tit = '';
		if(title) {
			tit = title;
		}
		gnUrl.openWebview(tit, rawUrl);
		return true;
	} else {
		return false;
	}
};

gnUrl.openWebview = function(tit, rawUrl) {
	mui.fire(plus.webview.getLaunchWebview(), 'rootOpenWebview', {
		tit: tit,
		pageUrl: rawUrl
	});
};

gnUrl.onUrlTap = function(e) {
	var href = this.getAttribute('href');
	gnUrl.exec(href);
	e.stopPropagation();
	return false;
};

gnUrl.bindEvent = function() {
	var btns = document.getElementsByClassName('gn-url-btn');
	for(var i = 0; i < btns.length; i++) {
		btns[i].removeEventListener('tap', gnUrl.onUrlTap);
		btns[i].addEventListener('tap', gnUrl.onUrlTap);
	}

};

gnApp.async = function(list, cb_exec, cb_end) {
	var each = function(_list, cb) {
		if(_list.length < 1) {
			return cb_end && cb_end();
		}
		cb(_list.shift(), function() {
			each(list, cb);
		})
	}
	each(list, cb_exec)
};

gnApp.getUid = function() {
	return Math.floor(Math.random() * 100000000 + 10000000).toString();
};

gnApp.TrigEvent = function(id, eventData) {
	plus.statistic.eventTrig(id, eventData);
};

// 对Date的扩展，将 Date 转化为指定格式的String   
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符，   
// 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)   
// 例子：   
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423   
// (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18   
Date.prototype.Format = function(fmt) { //author: meizz   
	var o = {
		"M+": this.getMonth() + 1, //月份   
		"d+": this.getDate(), //日   
		"h+": this.getHours(), //小时   
		"m+": this.getMinutes(), //分   
		"s+": this.getSeconds(), //秒   
		"q+": Math.floor((this.getMonth() + 3) / 3), //季度   
		"S": this.getMilliseconds() //毫秒   
	};
	if(/(y+)/.test(fmt))
		fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	for(var k in o)
		if(new RegExp("(" + k + ")").test(fmt))
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
	return fmt;
};

(function() {
	var btns = document.querySelectorAll(".common-page-btn");
	btns.forEach(function(el) {

		el.addEventListener('tap', function() {
			var hrefStr = this.getAttribute('data-href');
			var href = JSON.parse(hrefStr);
			var autoShow = true;
			if(href.autoShow == false) {
				autoShow = false;
			}
			if(href) {
				if(href.needLogin && !gnAuth.isLogin()) {
					gnAuth.openLogin();
				} else {
					gnApp.openPage(href.pageUrl, href.pageId, {}, autoShow);
				}
			}
		});
	});
})();

var HtmlUtil = {
	/*1.用浏览器内部转换器实现html转码*/
	htmlEncode: function(html) {
		//1.首先动态创建一个容器标签元素，如DIV
		var temp = document.createElement("div");
		//2.然后将要转换的字符串设置为这个元素的innerText(ie支持)或者textContent(火狐，google支持)
		(temp.textContent != undefined) ? (temp.textContent = html) : (temp.innerText = html);
		//3.最后返回这个元素的innerHTML，即得到经过HTML编码转换的字符串了
		var output = temp.innerHTML;
		temp = null;
		return output;
	},
	/*2.用浏览器内部转换器实现html解码*/
	htmlDecode: function(text) {
		//1.首先动态创建一个容器标签元素，如DIV
		var temp = document.createElement("div");
		//2.然后将要转换的字符串设置为这个元素的innerHTML(ie，火狐，google都支持)
		temp.innerHTML = text;
		//3.最后返回这个元素的innerText(ie支持)或者textContent(火狐，google支持)，即得到经过HTML解码的字符串了。
		var output = temp.innerText || temp.textContent;
		temp = null;
		return output;
	},
	/*3.用正则表达式实现html转码*/
	htmlEncodeByRegExp: function(str) {
		var s = "";
		if(str.length == 0) return "";
		s = str.replace(/&/g, "&amp;");
		s = s.replace(/</g, "&lt;");
		s = s.replace(/>/g, "&gt;");
		s = s.replace(/ /g, "&nbsp;");
		s = s.replace(/\'/g, "&#39;");
		s = s.replace(/\"/g, "&quot;");
		return s;
	},
	/*4.用正则表达式实现html解码*/
	htmlDecodeByRegExp: function(str) {
		var s = "";
		if(str.length == 0) return "";
		s = str.replace(/&amp;/g, "&");
		s = s.replace(/&lt;/g, "<");
		s = s.replace(/&gt;/g, ">");
		s = s.replace(/&nbsp;/g, " ");
		s = s.replace(/&#39;/g, "\'");
		s = s.replace(/&quot;/g, "\"");
		return s;
	}
};

String.prototype.format = function(args) {
	if(arguments.length > 0) {
		var result = this;
		if(arguments.length == 1 && typeof(args) == "object") {
			for(var key in args) {
				var reg = new RegExp("({" + key + "})", "g");
				result = result.replace(reg, args[key]);
			}
		} else {
			for(var i = 0; i < arguments.length; i++) {
				if(arguments[i] == undefined) {
					return "";
				} else {
					var reg = new RegExp("({[" + i + "]})", "g");
					result = result.replace(reg, arguments[i]);
				}
			}
		}
		return result;
	} else {
		return this;
	}
}

document.addEventListener("plusready", function() {
	setTimeout(function() {
		if(window.pageKey && plus) {
			winsetting = plus.webview.currentWebview();
			window.startDate = new Date();

			winsetting.addEventListener("close", function() {
				var endDate = new Date();
				var ms = endDate - window.startDate;
				plus.statistic.eventDuration(window.pageKey, ms, {
					"pageLabel": window.pageLabel || ''
				});
			}, false);

		}
	}, 500);
}, false);